﻿/// <reference path="../libs/underscore.js" />

//main controller
function MainController() {
}

//display all users
function UsersController($scope, $http, bookStoreData) {

    bookStoreData.getUsers().success(function (users) {
        $scope.users = users;
    });
}

//display single user details
function UserDetailsController($scope, $http, $routeParams, bookStoreData) {
    var id = $routeParams.id;

    //get user details
    bookStoreData.getUserDetails(id)
	.success(function (user) {
	    //console.log(user);
	    $scope.user = user;
	});
    
    //Give or take access
    $scope.changeAccessLevel = function () {
        var updatedProfile = {
            Id: $scope.user.Id,
            IsAdmin: !$scope.user.IsAdmin
        }

        bookStoreData.putUser(updatedProfile)
            .success(function () {
                $scope.user.IsAdmin = updatedProfile.IsAdmin;
            });
    }
}

//add new store
function CreateStoreController($scope, $http, bookStoreData) {
    $scope.newStore = {
        name: "",
        city: "",
        revenue: ""
    };

    //get all stores
    bookStoreData.getStores()
	.success(function (stores) {
	    console.log(stores);
	    $scope.stores = stores;
	});

    //add new store
    $scope.addStore = function () {
        console.log($scope.myHeaders);

        bookStoreData.postStore($scope.newStore)
	    .success(function () {
	        $scope.stores.push($scope.newStore);
	        $scope.newStore = {
	            name: "",
	            city: "",
	            revenue: ""
	        };
	    });
    }
}

//add new book
function CreateBookController($scope, $http, bookStoreData) {
    $scope.newBook = {
        title: "",
        genre: "",
        author: "",
        rentCost: "",
    };

    //get all stores
    bookStoreData.getStores()
	.success(function (stores) {
	    $scope.stores = stores;
	});

    //get all books
    bookStoreData.getBooks()
	.success(function (books) {
	    $scope.books = books;
	});

    //add new book
    $scope.addBook = function () {
        console.log($scope.newBook);
        bookStoreData.postBook($scope.newBook)
	    .success(function () {
	        $scope.books.push($scope.newBook);
	        $scope.newBook = {
	            title: "",
	            genre: "",
	            author: "",
	            rentCost: "",
	        };
	    });
    }
}